package com.example.checkqufuface.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.checkqufuface.R;
import com.example.checkqufuface.bean.QufuQueryCertificateResultBean;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class PeopleMessageAdapter extends RecyclerView.Adapter<PeopleMessageAdapter.ViewHolder> {

    private Context context;
    private List<QufuQueryCertificateResultBean.DataBean> dataList;
    private static String baseUrl;

    public PeopleMessageAdapter(Context context, List<QufuQueryCertificateResultBean.DataBean> dataList, String baseUrl) {
        this.context = context;
        this.dataList = dataList;
        this.baseUrl = baseUrl;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_people_message_show, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.tvName.setText(dataList.get(position).getCertificateName());
        holder.tvPhone.setText(dataList.get(position).getPhoneNumber());
        holder.tvIdTitle.setText(dataList.get(position).getCertificateTypeName());
        holder.tvId.setText(dataList.get(position).getCertificateNo());
        try {
//            holder.ivFace.setImageBitmap(getBitmap(dataList.get(position).getFacePicPath()));
            Glide.with(context)
                    .load(dataList.get(position).getFacePicPath())
                    .placeholder(R.drawable.shape_gray)
                    .into(holder.ivFace);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvName;
        private final TextView tvPhone;
        private final TextView tvId;
        private final TextView tvIdTitle;
        private final ImageView ivFace;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tvName = itemView.findViewById(R.id.tv_message_name);
            tvPhone = itemView.findViewById(R.id.tv_message_phone);

            tvIdTitle = itemView.findViewById(R.id.tv_message_id_title);
            tvId = itemView.findViewById(R.id.tv_message_id);

            ivFace = itemView.findViewById(R.id.iv_people_message_face);
        }
    }

    public static Bitmap getBitmap(String path) throws IOException {

        URL url = new URL(baseUrl + "/" +path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() == 200){
            InputStream inputStream = conn.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            return bitmap;
        }
        return null;
    }
}
